/*
 * TITLE:RequestToServerTable
 * AUTHOR:Manori Thakur
 * PURPOSE: Maintains a list of response codes and their respective classes for instantiation
 */
package clientcore;

import android.util.Log;
import clientresponse.ResponseFromServer;
import java.util.HashMap;

public class ResponseFromServerTable {
	private static final String TAG = ResponseFromServerTable.class.getSimpleName();
    private static HashMap<Short, Class> responseTable = null;

    public ResponseFromServerTable() {

    }

    public static void init(AndroidMain ac) {
        responseTable = new HashMap<Short, Class>();
        try {
            responseTable.put(Constants.SMSG_AUTH_RESPONSE, Class.forName("clientresponse." + "ResponseLogin"));
            responseTable.put(Constants.SMSG_DISCONNECT, Class.forName("clientresponse." + "ResponseLogout"));
            responseTable.put(Constants.SMSG_BOARD_QUESTION, Class.forName("clientresponse." + "ResponseBoardQuestion"));
            responseTable.put(Constants.SMSG_BOARD_ANSWER, Class.forName("clientresponse." + "ResponseBoardAnswer"));
            responseTable.put(Constants.SMSG_QUESTION_LOG, Class.forName("clientresponse." + "ResponseQuestionLog"));
            responseTable.put(Constants.SMSG_LOCAL_PLAYERS, Class.forName("clientresponse." + "ResponseLocalPlayers"));
            responseTable.put(Constants.SMSG_PUBLIC_CHAT, Class.forName("clientresponse." + "ResponseChatPublic"));
            responseTable.put(Constants.SMSG_PRIVATE_CHAT, Class.forName("clientresponse." + "ResponseChatPrivate"));
            responseTable.put(Constants.SMSG_SEND_PRIVATE_CHAT, Class.forName("clientresponse." + "ResponseUpdateChatPrivate"));
            responseTable.put(Constants.SMSG_GLOBAL_CHAT, Class.forName("clientresponse." + "ResponseChatGlobal"));
            responseTable.put(Constants.SMSG_BUDDIES, Class.forName("clientresponse." + "ResponseBuddies"));
            responseTable.put(Constants.SMSG_BUDDY_INVITE, Class.forName("clientresponse." + "ResponseBuddyInvite"));
            responseTable.put(Constants.SMSG_BUDDY_ADD, Class.forName("clientresponse." + "ResponseBuddyAccept"));
            responseTable.put(Constants.SMSG_BUDDY_REMOVE, Class.forName("clientresponse." + "ResponseBuddyRemove"));
            responseTable.put(Constants.SMSG_SEND_BUDDY_INVITES, Class.forName("clientresponse." + "ResponseUpdateBuddyInvites"));
            
        } catch (ClassNotFoundException ex) {
        	Log.e(TAG,"Class Not Found Exception in method init()"+ex.toString());
        }
    }

    public static ResponseFromServer get(short response_id) {
        ResponseFromServer response = null;

        try {
            Class name = responseTable.get(response_id);

            if (name != null) {
                response = (ResponseFromServer) name.newInstance();
            } else {
                System.out.println("Invalid Response Code: " + response_id);
            }
        } catch (Exception e) {
        	Log.e(TAG,"Exception in method init()"+e.toString());
        }

        return response;
    }
}
